/*
 * @Author: your name
 * @Date: 2020-08-02 14:09:07
 * @LastEditTime: 2020-08-30 20:57:16
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \project\羊绒产业园\源码\src\js\http\http.js
 */
/**
 * http配置
 */
// 引入axios以及element ui中的loading和message组件
import axios from 'axios'
import Router from 'vue-router'
import router from '../../router'
import store from '../../store'
import { Toast, Dialog } from 'vant'

// axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded;charset=UTF-8';
// 超时时间
// axios.defaults.timeout = 5000
    // http请求拦截器
var loadinginstace
axios.interceptors.request.use(config => {
        config.headers.Authorization = `${ store.state.userInfo.tokenHead }${ store.state.userInfo.ptToken }`
        config.headers.common["Access-Control-Allow-Credentials"] = true;
        config.headers.common["token"] = `${ store.state.userInfo.tokenHead }${ store.state.userInfo.ptToken }`;
       
        return config
    }, error => {
        // loadinginstace.close()
        // Element.Message.error({
        //   message: '加载超时'
        // })
        Toast.fail('加载超时');
        return Promise.reject(error)
    })
    // http响应拦截器
axios.interceptors.response.use(data => { // 响应成功关闭loading
    // loadinginstace.close();

    if (data.data.code != 200) {


        if (data.data.code == 403) {
            router.push({ path: '/Login' })
            Dialog.confirm({
                title: '提示',
                message: '登录失效，是否重新登录?'
            }).then(() => {
                // on confirm
                router.push({ path: '/Login' })
            }).catch(() => {
                // on cancel
            });

            return;
        }
        Toast.fail(data.data.msg);
    }

    return data
}, error => {
    // loadinginstace.close();
    Toast.fail('出错啦~');
    return Promise.reject(error)
})

export default axios